<template>
  <div>
    <p style="text-align:right">
      <el-button type="primary"  @click="$router.push({path: '/goods/listOfAllGoods'})">返回商品列表</el-button>
    </p>
    <!-- 基本信息填写 -->
    <goods-base-info-setting
      ref="goodsBaseInfoSetting"
      @categoryIdChange='setCategoryId'
    ></goods-base-info-setting>
    <!-- 商品sku设定 -->
    <goods-sku-setting
      :categoryid = 'categoryId'
      ref='goodsSkuSetting'
    ></goods-sku-setting>
    <!-- 首页占位图 -->
    <goods-pic-upload-for-index
      ref='goodsPicUploadForIndex'
    ></goods-pic-upload-for-index>
    <!-- 物流信息设置 -->
    <goods-logistics-setting
      ref='goodsLogisticsSetting'
    ></goods-logistics-setting>

    <!-- 商品设置上下架 -->
    <goods-up-and-down
      ref='goodsUpAndDown'
    ></goods-up-and-down>

    <el-button type="primary" @click="onSubmit">立即创建</el-button>
    <el-button>取消</el-button>
  </div>
</template>

<script>
  import goodsSkuSetting from 'components/goods/goodsSkuSetting.vue'
  import goodsPicUploadForIndex from 'components/goods/goodsPicUploadForIndex.vue'
  import goodsBaseInfoSetting from 'components/goods/goodsBaseInfoSetting.vue'
  import goodsLogisticsSetting from 'components/goods/goodsLogisticsSetting.vue'
  import goodsUpAndDown from 'components/goods/goodsUpAndDown.vue'
  import {
    saveGoodsInfo
  } from 'src/api/index.js'
  export default {
    data () {
      return {
        categoryId: null
      }
    },
    methods: {
      formValidation () {
        // 各表单校验
        let baseInfoValidation = this.$refs.goodsBaseInfoSetting.formValidation
        let skuSettingValidation = this.$refs.goodsSkuSetting.formValidation
        let logisticsSettingValidation = this.$refs.goodsLogisticsSetting.formValidation
        let picUploadValidation = this.$refs.goodsPicUploadForIndex.formValidation
        let upAndDownValidation = this.$refs.goodsUpAndDown.formValidation
        let condition =
          baseInfoValidation() &&
          skuSettingValidation() &&
          logisticsSettingValidation() &&
          picUploadValidation() &&
          upAndDownValidation()
        if (condition) {
          return 1
        }
        return 0
      },
      onSubmit () {
        let that = this
        let result = this.formValidation()
        if (result) {
          let param = {
            ...this.$refs.goodsBaseInfoSetting.getFormData(),
            ...this.$refs.goodsSkuSetting.getFormData(),
            ...this.$refs.goodsLogisticsSetting.getFormData(),
            ...this.$refs.goodsPicUploadForIndex.getFormData(),
            ...this.$refs.goodsUpAndDown.getFormData()
          }
          let loadding = this.$loading()
          saveGoodsInfo(param).then((res) => {
            loadding.close()
            let result = res.data
            if (result.code === 100) {
              this.$alert('添加成功', '提示', {
                callback () {
                  that.$router.push({
                    path: '/goods/listOfAllGoods'
                  })
                }
              })
            } else {
              this.$message.error(result.message)
            }
          })
        }
      },
      setCategoryId (id) {
        this.categoryId = id
      }
    },
    components: {
      goodsSkuSetting,
      goodsPicUploadForIndex,
      goodsBaseInfoSetting,
      goodsLogisticsSetting,
      goodsUpAndDown
    }
  }
</script>

<style lang='less' scoped>
  .p_hr{
    padding-bottom: 20px;
    border-bottom: 1px solid #f0f0f0;
  }
</style>
